package cas.ihep.pwa.fcn;

import cas.ihep.pwa.api.IGlobalSet;
import cas.ihep.pwa.api.IRunningParam;
import cas.ihep.pwa.evtgen.EvtPDL;

import java.io.Serializable;
import java.util.List;

public class SparkFCN implements Serializable{
    public static String equpar(String name,IGlobalSet gset){
        String thename="";
        int sz=gset.pareqlSize();
        for(int i=0;i<sz;i++){
            String str=gset.pareql(i);
            if (str.isEmpty()){
                break;
            }
            if (str.equals(name)){
                thename=gset.pareqr(i);
            }
        }
        return thename;
    }

    public static int getSampleIndex(List<Integer> setsample,int ievt){
        int idx=setsample==null?0:setsample.size();
        int first;
        if (idx<=0 || ievt<(first=setsample.get(0))){
            return 0;
        }
        int bin0=first;
        int bin1=bin0;
        for(int i=1;i<idx;i++){
            bin1+=setsample.get(i);
            if(bin0<=ievt && ievt<bin1){
                return i;
            }
        }
        throw new RuntimeException("No sample index are found. setsample.size()= "+idx);
    }

    public static int getPar(IRunningParam rp,String name,IGlobalSet gset){
        String thename=equpar(name,gset);
        if (!thename.isEmpty()){
            name=thename;
        }
        int len=rp.theparnameSize();
        for(int i=0;i<len;i++){
            if (name.equals(rp.theparname(i))){
                return i;
            }
        }
        throw new RuntimeException("No paramerter found in the parameter list matches "+name);
    }

    public static void preset(double vpars[],EvtPDL pdl,IRunningParam rp,IGlobalSet gset,List<Double> rval){
        if (!gset.PipiSwavePhase().isEmpty()){
            String myphase=gset.PipiSwavePhase();
            int paridx=getPar(rp,myphase,gset);
            rval.add(vpars[paridx]);
            int resid=pdl.getId("PipiSWave");
            List<Double> mypars=pdl.getVpars(resid);
            if(mypars.get(0)!=vpars[paridx]){
                rp.setYesbw(true);
            }
            pdl.setVpars(resid,rval);
            rval.clear();
        }
        int tmpsize=rp.addMassSize();
        for(int i=0;i<tmpsize;i++){
            String resname=rp.addMass(i,0);
            int resid=pdl.getId(resname);
            String parname=rp.addMass(i,1);
            int paridx=getPar(rp,parname,gset);
            double xmass=vpars[paridx];
            double xm0=pdl.getMeanMass(resid);
            if(xmass!=xm0){
                rp.setYesbw(true);
            }
            pdl.reSetMass(resid,xmass);
        }
        tmpsize=rp.addWidthSize();
        for(int i=0;i<tmpsize;i++){
            String resname=rp.addWidth(i,0);
            int resid=pdl.getId(resname);
            String parname=rp.addWidth(i,1);
            int paridx=getPar(rp,parname,gset);
            double xmass=vpars[paridx];
            double xg0=pdl.getWidth(resid);
            if(xmass!=xg0){
                rp.setYesbw(true);
            }
            pdl.reSetWidth(resid,xmass);
        }
        tmpsize=rp.addReBwSize();
        for(int i=0;i<tmpsize;i++){
            String resname=rp.addReBw(i,0);
            int resid=pdl.getId(resname);
            String parname=rp.addReBw(i,1);
            int paridx=getPar(rp,parname,gset);
            double xmass=vpars[paridx];
            double xm0=pdl.getMeanMass(resid);
            if(xmass!=xm0){
                rp.setYesbw(true);
            }
            pdl.reSetMass(resid,xmass);
        }
        tmpsize=rp.addImBwSize();
        for(int i=0;i<tmpsize;i++){
            String resname=rp.addImBw(i,0);
            String parname=rp.addImBw(i,1);
            int resid=pdl.getId(resname);
            int paridx=getPar(rp,parname,gset);
            double xmass=vpars[paridx];
            double xg0=pdl.getWidth(resid);
            if(xmass!=xg0){
                rp.setYesbw(true);
            }
            pdl.reSetWidth(resid,xmass);
        }
        tmpsize=rp.addSpecialBWSize();
        for(int i=0;i<tmpsize;i++){
            String resname=rp.addSpecialBW(i,0);
            int resid=pdl.getId(resname);
            List<String> adds=rp.addSpecialBW(i);
            int sz=adds.size();
            for(int j=2;j<sz;j++){
                String parname=adds.get(j);
                int paridx=getPar(rp,parname,gset);
                rval.add(vpars[paridx]);
            }
            List<Double> mypars=pdl.getVpars(resid);
            if(mypars.size()==2){
                double  my0=mypars.get(0),my1=mypars.get(1),rval0=rval.get(0),rval1=rval.get(1);
                if (my0!=rval0 || my1!=rval1){
                    rp.setYesbw(true);
                }
            }
            pdl.setVpars(resid,rval);
            rval.clear();
        }
        if (rp.addArgReSize()>0 && rp.addArgReSize()!=rp.addArgImSize()){
            throw new RuntimeException();
        }
        tmpsize=rp.addArgReSize();
        for(int i=0;i<tmpsize;i++){
            String resname=rp.addArgRe(i,0);
            String resname2=rp.addArgIm(i,0);
            if (!resname.equals(resname2)){
                throw new RuntimeException();
            }
            if (rp.addArgRe(i).size()<5){
                throw new RuntimeException();
            }
            int resid=pdl.getId(resname);
            rval.add(Double.parseDouble(rp.addArgRe(i,2)));
            rval.add(Double.parseDouble(rp.addArgRe(i,3)));
            for(int j=4;j<rp.addArgRe(i).size();j++){
                String parname=rp.addArgRe(i,j);
                String parname2=rp.addArgIm(i,j);
                int paridx=getPar(rp,parname,gset);
                int paridx2=getPar(rp,parname2,gset);
                rval.add(vpars[paridx]);
                rval.add(vpars[paridx2]);
            }
            pdl.setVpars(resid,rval);
            rval.clear();
        }
        tmpsize=rp.addPhaseShiftSize();
        for(int i=0;i<tmpsize;i++){
            if(rp.addPhaseShift(i).size()<7){
                throw new RuntimeException();
            }
            String resname=rp.addPhaseShift(i,0);
            String son1=rp.addPhaseShift(i,2);
            String son2=rp.addPhaseShift(i,3);
            int resid=pdl.getId(resname);
            int son1id=pdl.getId(son1);
            int son2id=pdl.getId(son2);
            double mson1=pdl.getMeanMass(son1id);
            double mson2=pdl.getMeanMass(son2id);
            rval.add(mson1);
            rval.add(mson2);
            rval.add(Double.parseDouble(rp.addPhaseShift(i,4)));
            rval.add(Double.parseDouble(rp.addPhaseShift(i,5)));
            for(int j=6;j<rp.addPhaseShift(i).size();j++){
                String parname=rp.addPhaseShift(i,j);
                int paridx=getPar(rp,parname,gset);
                rval.add(vpars[paridx]);
            }
            pdl.setVpars(resid,rval);
            rval.clear();
        }
    }

}
